#include <iostream>

using namespace std;

/*
 * 通讯录功能
 *      1、添加联系人，信息(姓名，性别，年龄，联系电话，家庭住址) 最多记录1000人
 *      2、显示联系人
 *      3、删除联系人
 *      4、查找联系人
 *      5、修改联系人
 *      6、清空联系人
 *      7、退出通讯录
 */

// 可以考虑封装一个通讯录类

struct Address{
    string name;
    string sex;
    int age;
    string telephone;
    string homeAddress;
};

void printMeau() {
    cout << "*************************************************" << endl;
    cout << "*                   选择功能:                    *" << endl;
    cout << "*                 1、添加联系人                   *" << endl;
    cout << "*                 2、显示联系人                   *" << endl;
    cout << "*                 3、删除联系人                   *" << endl;
    cout << "*                 4、查找联系人                   *" << endl;
    cout << "*                 5、修改联系人                   *" << endl;
    cout << "*                 6、清空联系人                  *" << endl;
    cout << "*                 7、退出通讯录                  *" << endl;
    cout << "************************************************" << endl;
}

// 接受外部功能输入并做简单的过滤功能
int selectFunction() {
    int inputNum;
    cin >> inputNum;
    if(inputNum<1 || inputNum >7){
        cout << "无效输入，请重新输入" << endl;
        return -1;
    } else{
        return inputNum;
    }
}

//根据输入组装一个address对象
Address newAddress() {
    struct Address newAddress;
    cout << "请输入联系人姓名:";
    cin >> newAddress.name;
    cout << "请输入联系人性别:";
    cin >> newAddress.sex;
    cout << "请输入联系人年龄:";
    cin >> newAddress.age;
    cout << "请输入联系人电话:";
    cin >> newAddress.telephone;
    cout << "请输入联系人住址:";
    cin >> newAddress.homeAddress;
    return newAddress;
}

// 添加联系人
int addNewAddress(struct Address addressList[], int index) {
    if(index >= 999) {
        cout << "通讯录已满" << endl;
        return index;
    }
    Address address = newAddress();
    addressList[index++] = address;
    cout << "添加成功" << endl;
    return index;
}

void showAddress(Address address) {
    cout << address.name << "\t" << address.sex << "\t" << address.age << "\t" << address.telephone << "\t" << address.homeAddress << endl;
}

// 显示联系人
void showAllAddress(struct Address addressList[], int index) {
    if(index == 0) {
        cout << "当前无联系人" << endl;
        return;
    }
    for (int i = 0; i < index; ++i) {
//        cout << addressList[i].name << "\t" << addressList[i].sex << "\t" << addressList[i].age << "\t" << addressList[i].telephone << "\t" << addressList[i].homeAddress << endl;
        showAddress(addressList[i]);
    }
}

//查找联系人
int searchAddress(struct Address addressList[], int index) {
    string addressName;
    cout << "请输入联系人姓名:";
    cin >> addressName;
    for (int i = 0; i < index; ++i) {
        if(addressList[i].name == addressName) {
            showAddress(addressList[i]);
            return i;
        }
    }
    cout << "查无此人" << endl;
    return -1;
}

// 删除联系人
int deleteAddress(struct Address addressList[], int index) {
    int addressIndex = searchAddress(addressList, index);
    if(addressIndex == -1){
        return index;
    }
    for (int i = addressIndex; i < index; ++i) {
        addressList[i] = addressList[i+1];
    }
    cout << "删除成功" << endl;
    return --index;
}

//修改联系人
void modifyAddress(struct Address addressList[], int index) {
    int addressIndex = searchAddress(addressList, index);
    if(addressIndex == -1){
        return;
    }
    addressList[addressIndex] = newAddress();
    cout << "修改成功" << endl;
}

//清空联系人
void deleteAllAddress(struct Address addressList[], int index) {
    Address empty;
    for (int i = 0; i < index; ++i) {
        addressList[i] = empty;
    }
    cout << "清空成功" << endl;
}

int main() {
    // 联系人结构体，最多存储1000人
    struct Address addressList[1000];
    // 储存数组索引，当前应该插入的下标，删除时往前走一位删除
    int index = 0;
    bool isLoop = true;
    while (isLoop) {
        printMeau();
        int inputFunction = selectFunction();
        switch (inputFunction) {
            case 1:
                index = addNewAddress(addressList, index);
                break;
            case 2:
                showAllAddress(addressList, index);
                break;
            case 3:
                index = deleteAddress(addressList, index);
                break;
            case 4:
                searchAddress(addressList, index);
                break;
            case 5:
                modifyAddress(addressList, index);
                break;
            case 6:
                deleteAllAddress(addressList, index);
                index = 0;
                break;
            case 7:  // 退出功能
                isLoop = false;
                break;
            default:
                cout << "出bug了" << endl;
                break;
        }
    }
    return 0;
}
